<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
  <head>
    <meta charset="utf-8" />
    <meta name="generator" content="pandoc" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, user-scalable=yes"
    />
    <title>readme</title>
    <style type="text/css">
      code {
        white-space: pre-wrap;
      }
      span.smallcaps {
        font-variant: small-caps;
      }
      span.underline {
        text-decoration: underline;
      }
      div.column {
        display: inline-block;
        vertical-align: top;
        width: 50%;
      }
    </style>
  </head>
  <body>
    <h1 id="practice-exercises-for-organizing-with-functions">
      Practice exercises for organizing with functions
    </h1>
    <h2 id="overview">Overview</h2>
    <p>
      In this section, you’ll have a chance to practice the concepts you’ve
      learned in the videos. First, review the core concepts covered that you’ll
      need to keep in mind. Then go through the exercises below.
    </p>
    <p>
      Remember, these are for your own benefit. Feel free to skip them if you
      don’t find a particular exercise valuable or you get stuck for too long.
    </p>
    <h2 id="core-concepts">Core concepts</h2>
    <h3 id="function-with-parameters-input-data">
      Function with parameters (input data)
    </h3>
    <p>
      When we have code that we want to reuse or isolate and treat as a single
      concept (black box), we define functions. Here is a basic function:
    </p>
    <pre><code>def say_hello(name):
    print(f&#39;Nice to meet you {name}&#39;)</code></pre>
    <h3 id="function-that-generates-data-return-values">
      Function that generates data (return values)
    </h3>
    <p>Functions can also return values to be used later:</p>
    <pre><code>def get_name():
   name = input(&quot;What is your name? &quot;)
   return name</code></pre>
    <p>We can then use these functions together as follows:</p>
    <pre><code>person = get_name()
say_hello(person)</code></pre>
    <h3 id="a-main-method-and-running-the-program">
      A main method and running “the program”
    </h3>
    <p>
      As we saw, it’s a good convention to have an overall organizing function
      that is what the whole program does at the top of the file. I called this
      main, for example:
    </p>
    <pre><code>def main():
    show_header()
    get_names()
    # ...</code></pre>
    <p>
      And you must remember to run this <strong>at the end</strong> of your
      program to actually make it execute. We added these two lines as the final
      of the source file:
    </p>
    <pre><code>if __name__ == &quot;__main__&quot;:
    main()</code></pre>
    <h2 id="exercises">Exercises</h2>
    <p>
      Now it’s your turn. Your practice exercise is to
      <strong
        >take the M&amp;M guessing game we created back in chapter 5
        (interactive code) and clean it up using functions</strong
      >. Make a copy of the file in this folder called
      <code>guessinggame.py</code>. That’s what we started with. There are
      probably 3-4 functions that you can create to help organize and isolate
      parts of this application. Use the core concepts above to help.
    </p>
  </body>
</html>
